﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开单.Dao.基础;
using 开单.Model.基础;
using 开单.ComMethod;
using 开单.DB;

namespace 开单.Frm.基础
{
    public partial class FrmKhxx : Form
    {
        KhxxDao khxxDao = new KhxxDao();
        KhxxEntity khxxEntity = new KhxxEntity();
        YgxxDao ygxxDao = new YgxxDao();
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        private static FrmKhxx Instance;
        private FrmKhxx()
        {
            InitializeComponent();
            this.uClistMoreKh.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreKh_ListEvent);
            uClistMoreYg.ListEvent += new ClientApplication.ComControl.UClistMore.ListEventHandler(uClistMoreYg_ListEvent);
            RefeashDtv();
        }

        void uClistMoreYg_ListEvent(MyListItem myList)
        {
            TxtYg.Text = myList.TextField;
            TxtYg.Tag = myList.ValueField;
            uClistMoreYg.Visible = false;
            TxtYg.Focus();
        }

        public static FrmKhxx GetInstance()
        {
            if (null == Instance || Instance.IsDisposed)
            {
                Instance = new FrmKhxx();
                //Instance.MdiParent = GlobalVar.frmMain;
            }

            Instance.BringToFront();
            return Instance;
        }

        void uClistMoreKh_ListEvent(开单.ComMethod.MyListItem myList)
        {
            TxtKh.Text = myList.TextField;
            TxtKh.Tag = myList.ValueField;
            uClistMoreKh.Visible = false;
            TxtKh.Focus();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.RowCount == 0) { return; }
            if (e.RowIndex == -1) { return; }
            TxtKh.Tag = dataGridView1[0, e.RowIndex].Value;
            TxtKhName.Text = dataGridView1[1, e.RowIndex].Value.ToString();
            TxtYg.Text = dataGridView1[2, e.RowIndex].Value.ToString();
            TxtKhdh.Text = dataGridView1[3, e.RowIndex].Value.ToString();
            TxtKhdz.Text = dataGridView1[4, e.RowIndex].Value.ToString();
            TxtKhjp.Text = dataGridView1[5, e.RowIndex].Value.ToString();
            groupBox3.Visible = true;
            button2.Visible = false;
            button1.Visible = true;
            toolStripDel.Visible = true;
        }

        private void toolStripRefeash_Click(object sender, EventArgs e)
        {
            RefeashDtv();
            groupBox3.Visible = false;
        }

        private void RefeashDtv()
        {
            DataTable dtAll = khxxDao.GetKhxxDt("");
            dataGridView1.DataSource = dtAll;
            dtAll.Dispose();
        }

        private void toolStripQuery_Click(object sender, EventArgs e)
        {
            if (null != dt)
            {
                dt = khxxDao.GetKhxxDtByKhName(TxtKh.Text.Trim());
                dataGridView1.DataSource = dt;
            }
        }

        private void TxtKh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt.Rows.Count)
                {
                    if (dt.Rows[0][1].ToString() == TxtKh.Text) { return; }
                }
                dt = khxxDao.GetKhxxDt(TxtKh.Text.Trim());
                uClistMoreKh.addData(dt);
                uClistMoreKh.BringToFront();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要修改吗？", "提示", MessageBoxButtons.YesNo,
                      MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            khxxEntity.kh_code = Convert.ToInt32(TxtKh.Tag.ToString());
            khxxEntity.kh_name = TxtKhName.Text;
            khxxEntity.yg_name = TxtYg.Text;
            khxxEntity.kh_dh = TxtKhdh.Text;
            khxxEntity.kh_dz = TxtKhdz.Text;
            khxxEntity.kh_jp = TxtKhjp.Text;
            khxxDao.UpdataKhxxByKh_code(khxxEntity);
            RefeashDtv();
            MessageBox.Show("修改成功");
            IntityControl();
            groupBox3.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!(IsOKToAdd())) { return; };
            if (!(MessageBox.Show("确定要增加吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)) { return; }
            //khxxEntity.kh_code = Convert.ToInt32(TxtKh.Tag.ToString());
            khxxEntity.kh_name = TxtKhName.Text;
            khxxEntity.yg_name = TxtYg.Text;
            khxxEntity.kh_jp = TxtKhjp.Text;
            khxxEntity.kh_dh = TxtKhdh.Text;
            khxxEntity.kh_dz = TxtKhdz.Text;
            khxxDao.AddKhxx(khxxEntity);
            RefeashDtv();
            MessageBox.Show("增加成功");
            IntityControl();
            TxtKhName.Focus();
        }

        private bool IsOKToAdd()
        {
            if (TxtKhName.Text == "") { MessageBox.Show("请先输入客户名"); TxtKhName.Focus(); return false; }
            if (TxtKhdh.Text == "") { MessageBox.Show("请输入客户电话"); TxtKhdh.Focus(); return false; }
            if (TxtKhdz.Text == "") { MessageBox.Show("请输入客户地址"); TxtKhdz.Focus(); return false; }
            if (TxtYg.Text == "" || TxtYg.Tag==null) { MessageBox.Show("请选择业务员"); TxtKhdz.Focus(); return false; }
            return true;
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            groupBox3.Visible = true;
            button2.Visible = true;
            button1.Visible = false;
            TxtKhName.Focus();
            IntityControl();
        }

        private void IntityControl()
        {
            TxtKhName.Text = "";
            TxtKhjp.Text = "";
            TxtKhdh.Text = "";
            TxtKhdz.Text = "";
            TxtYg.Text = "";
        }

        private void toolStripDel_Click(object sender, EventArgs e)
        {
            if (!(MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo,
            MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)) { return; }
            if (dataGridView1.RowCount == 0 || dataGridView1.CurrentCell.RowIndex == -1) { return; }
            khxxDao.DelKhxx(TxtKh.Tag);
            RefeashDtv();
            MessageBox.Show("删除成功");
            groupBox3.Visible = false;
            IntityControl();
        }

        private void toolStripClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtKhName_TextChanged(object sender, EventArgs e)
        {
            this.TxtKhjp.Text = GetPYString.GetPYStr(TxtKhName.Text);
        }

        private void TxtYg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (0 != dt2.Rows.Count)
                {
                    if (dt2.Rows[0][1].ToString() == TxtYg.Text) { return; }
                }
                dt2 = ygxxDao.GetYgxxDt(TxtYg.Text.Trim());
                uClistMoreYg.addData(dt2);
            }
        }

        private void button2_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void FrmKhxx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Alt && e.KeyCode == Keys.S)
            {
                button2_Click(null, null);
                MessageBox.Show("");
            }
        }
    }
}
